import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { casbinRole } from './casbinRole.entity';
import { casbinRule } from './casbinRule.entity';
import { log } from './log.entity'
import { userInfo } from './userInfo.entity';
import { DataSource } from 'typeorm';
import { addressInfo } from './addressInfo.entity';
import { ScheduleModule } from '@nestjs/schedule';
import { commodityInfo } from './commodityInfo.entity';
import { orderInfo } from './orderInfo.entity';
import { userInfoCenterSync } from './userInfoCenterSync.entity';
import { PanelModule } from './modules/panel/panel/panel.module';
import { AuthModule } from './auth/auth.module';
import { APP_GUARD } from '@nestjs/core';
import { jwtAuthGuard } from './common/jwt-auth.grard';
import { ConfigModule } from './config.module';
import * as casbin from 'casbin';
import { AuthZModule, AUTHZ_ENFORCER } from 'nest-authz';
import { ConfigService } from './config.service';

@Module({
  imports: [ConfigModule,
    AuthZModule.register({
      imports: [ConfigModule],
      enforcerProvider: {
        provide: AUTHZ_ENFORCER,
        useFactory: async (configSrv: ConfigService) => {
          const config = await configSrv.getAuthConfig();
          return casbin.newEnforcer(config.model, config.policy);
        },
        inject: [ConfigService],
      },
      usernameFromContext: (ctx) => {
        const request = ctx.switchToHttp().getRequest();
        return request.user && request.user.username;
      }
    }),ScheduleModule.forRoot(),TypeOrmModule.forRoot({  
    type: 'mongodb',  
    url: 'mongodb://47.115.220.9:27017/center',   
    useNewUrlParser: true,  
    useUnifiedTopology: true,  
    entities: [casbinRole, casbinRule, log, userInfo, addressInfo, commodityInfo, orderInfo, userInfoCenterSync], // 指定实体类的位置  
    synchronize: true, // 设置是否自动同步实体类与数据库  
  }), PanelModule, AuthModule],
  controllers: [AppController],
  providers: [AppService,  {
    provide: APP_GUARD,
    useClass: jwtAuthGuard
  }],
})
export class AppModule {
  constructor(private dataSource: DataSource) {}
}
